package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.CompanyForumAnswer;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.utils.FileUtil;
import com.isunland.managesystem.utils.LogUtil;

/**
 * 个人信息
 *
 * @author wu
 */
public class CompanyForumPersonMessageFragment extends Fragment {

  public static final String EXTRA_VALUE = "com.isunland.managesystem.ui.EXTRA_VALUE";
  private ImageView mImageForumPersonInfo;//头像
  private TextView mNameForumPersonInfo;//名字
  private String mName;
  private TextView mSignatureForumPersonInfo;//个性签名

  private TextView mMobileForumPersonInfo;//电话
  private String mPhoneNumber;
  private TextView mCompanyForumPersonInfo;//公司
  private String mCompany;
  private TextView mDepartmentForumPersonInfo;//部门
  private String mDeaprtment;
  private TextView eMmailForumPersonInfo;//邮箱
  private String mEmail;
  private CompanyForumAnswer.actualObject mTopicDetail;
  private CurrentUser mCurrentUser;
  private ImageLoader mImageLoader;
  private String mPicture;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.detailsMessage);
    mCurrentUser = CurrentUser.newInstance(getActivity());
    mTopicDetail = (CompanyForumAnswer.actualObject) getArguments().get(EXTRA_VALUE);
    if (mTopicDetail != null) {
      mName = mTopicDetail.getRegStaffName();
      mPicture = mTopicDetail.getPicture();
      LogUtil.i("111111111111" + mName);
      mPhoneNumber = mTopicDetail.getMobile();
      mCompany = mCurrentUser.getMemberName();
      mDeaprtment = mTopicDetail.getDeptName();
      mEmail = mTopicDetail.getEmail();
    }
    mImageLoader = RequestManager.getImageLoader();
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_forum_personal_info, container, false);
    mImageForumPersonInfo = (ImageView) view.findViewById(R.id.iv_image_forumPersonInfo);
    mNameForumPersonInfo = (TextView) view.findViewById(R.id.tv_name_forumPersonInfo);
    mSignatureForumPersonInfo = (TextView) view.findViewById(R.id.tv_signature_forumPersonInfo);
    mMobileForumPersonInfo = (TextView) view.findViewById(R.id.tv_mobile_forumPersonInfo);
    mCompanyForumPersonInfo = (TextView) view.findViewById(R.id.tv_company_forumPersonInfo);
    mDepartmentForumPersonInfo = (TextView) view.findViewById(R.id.tv_department_forumPersonInfo);
    eMmailForumPersonInfo = (TextView) view.findViewById(R.id.tv_email_forumPersonInfo);
    initData();
    return view;
  }

  private void initData() {
    mNameForumPersonInfo.setText(mName);
    mSignatureForumPersonInfo.setText("");
    mMobileForumPersonInfo.setText(mPhoneNumber);
    mCompanyForumPersonInfo.setText(mCompany);
    mDepartmentForumPersonInfo.setText(mDeaprtment);
    eMmailForumPersonInfo.setText(mEmail);
    if (!TextUtils.isEmpty(mPicture)) {
      mPicture = FileUtil.getThumbNailFilePath(mPicture);
      String urlImage = ApiConst.getDownLoadUrlByGet(mPicture);

      ImageListener listener = ImageLoader.getImageListener(mImageForumPersonInfo, // 控件名
          R.drawable.photo, R.drawable.photo); // 默认头像
      mImageLoader.get(urlImage, listener);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  public static Fragment newInstance(CompanyForumAnswer.actualObject topicContent) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VALUE, topicContent);
    CompanyForumPersonMessageFragment fragment = new CompanyForumPersonMessageFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
